﻿'==========================================================
'Module Name: 系统错误日志写入模块;
'Function:    抓取各个方法的错误信息;
'Description: 写入错误日志信息;
'==========================================================
Imports System.IO

Module Mod_ErrorLog

    Public Sub ErrInfoHandle(ByVal ModName As String, _
                             ByVal funName As String, _
                             ByVal ErrorMsg As String, _
                             ByVal ErrorSorce As String, _
                             Optional ByVal IsShowMsgBox As Boolean = False)
        IsShowMsgBox = False
        If IsShowMsgBox Then

            MsgBox("系统错误信息：" + "模块名称," + ModName + ";  " + Chr(13) + _
                                      "函数名称:" + funName + ";  " + Chr(13) + _
                                      "错误信息:" + ErrorMsg + "; " + Chr(13) + _
                                      "错误源," + ErrorSorce _
                   , MsgBoxStyle.Exclamation, "马鞍山视觉检测系统")
        Else
            WriteErrorLog(ModName, funName, ErrorMsg, ErrorSorce, 1)
        End If
    End Sub

    Public Sub WriteErrorLog(ByVal ModName As String, ByVal funName As String, ByVal ErrorMsg As String, ByVal ErrorSorce As String, Optional ByVal LogType As Integer = 0)
        Try

            Dim strTxt As String            '错误文本内容
            'Dim strLastNum As String        '错误文本最后的序号
            Dim strError As String          '当前错误信息字符串
            Dim LogDoc As String


            strError = CStr(Now) & "  模块名称:" & ModName & ";  函数名称:" & funName & ";  错误信息:" & ErrorMsg & ";  错误源:" & ErrorSorce

            If LogType = 0 Then
                LogDoc = "err"      '错误
            Else
                LogDoc = "log"      '日志
            End If

            Dim Path As String = My.Application.Info.DirectoryPath & "\" & LogDoc
            If Dir(Path, FileAttribute.Directory) = "" Then
                MkDir(Path)
            End If

            '错误文件名称和路径
            Dim fileName As String = Format(Now, "yyyy-MM-dd")
            Dim filePath As String = My.Application.Info.DirectoryPath & "\" & LogDoc & "\" & fileName + ".txt"

            '检查文件,不存在生成新的
            Dim SIO_File As New FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Read)
            SIO_File.Close()
            SIO_File.Dispose()
            'If File.Exists(filePath) = False Then
            '    File.Create(filePath)
            'End If

            '读取一个文件内容 
            Dim SIO_FileRead As New StreamReader(filePath)
            strTxt = SIO_FileRead.ReadToEnd()
            SIO_FileRead.Close()
            SIO_FileRead.Dispose()

            '生辰一个新的文件内容
            If Trim(strTxt) = "" Then
                strTxt = strError
            Else
                strTxt = strTxt + vbCrLf + vbCrLf + strError
            End If

            '写入一个新的文件内容
            Dim SIO_FileWrite As New StreamWriter(filePath)
            SIO_FileWrite.Write(strTxt)
            SIO_FileWrite.Close()
            SIO_FileWrite.Dispose()

        Catch ex As Exception

        End Try

    End Sub

End Module
